package com.jae.security.handle;

import com.jae.model.ResultCode;
import com.jae.model.ResultMsg;
import com.jae.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * token失效返回结果
 */
@Component
@Slf4j
public class OauthResourceAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        ResultCode resultCode = ResultCode.TOKEN_INVALID;
        if(e instanceof InsufficientAuthenticationException){
            resultCode = ResultCode.NO_TOKEN;
        }
        ResponseUtils.result(response,new ResultMsg(resultCode.getCode(), false,resultCode.getMessage()));
    }
}
